/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../data_struct/head.h"
#include "../helper_objs/head.h"
#include "../config/head.h"
#include "../impl/head.h"
#include "../macro/head.h"
#include "../msg/head.h"
#include "response.h"

namespace xos_http
{

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    static response::ITEM_DATA xos_item_data;

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    response::response()
    {
        init_data();
    }

    response::~response()
    {}

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int response::get_xos_item_data( ITEM_DATA*& item_ptr )
    {
        int ret = 0;
        xos_item_data.set_container( mgr::container() );
        item_ptr = &xos_item_data;
        return ret;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int response::set_connect( http::i_connect * pConnect )
    {
        int ret = 0;
        m_pConnect = (connection*)pConnect;
        return ret;
    }

    http::i_connect * response::get_connect()
    {
        return m_pConnect;
    }

    xos_box::i_str_property * response::tags()
    {
        return m_pTags;
    }

    xos_box::i_list * response::send_list()
    {
        return m_pSendList;
    }

    xos_box::i_list * response::body_list()
    {
        return m_pBodyList;
    }

    xos_box::i_list * response::cookies()
    {
        return m_pCookies;
    }

    int response::compose( xos_box::i_msg *& pMsg )
    {
        int ret = 0;
        m_pHttpCompose->proc();
        m_pConnect->fast_notify()->notify( pMsg, FAST_SEND );
        return ret;
    }

    xos_box::i_str_property * response::prop()
    {
        return m_pProperty;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int response::init_data()
    {
        int ret = 0;

        m_pHttpCompose = 0;
        m_pConnect = 0;
        m_pTask = 0;

        m_pTags = 0;
        m_pSendList = 0;
        m_pBodyList = 0;
        m_pCookies = 0;

        m_pProperty = 0;

        return ret;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //

    int response::init()
    {
        int ret = 0;
        if( 0 == ret )
        {
            m_pTags = mgr::container()->box()->get_str_prop();
            m_pSendList = mgr::container()->box()->list();
            m_pBodyList = mgr::container()->box()->list();
            m_pCookies = mgr::container()->box()->list();
        }
        if( 0 == ret )
        {
            m_pHttpCompose = mgr::container()->http()->compose_return();
            m_pHttpCompose->set_tag_property( m_pTags );
            m_pHttpCompose->set_send_list( m_pSendList );
            m_pHttpCompose->set_body_list( m_pBodyList );
            m_pHttpCompose->set_cookie_list( m_pCookies );
        }
        if( 0 == ret )
        {
            xos_box::i_str_property * pTags = tags();
            pTags->set( xos_http_protocol::HTTP_REQUEST_RET_CODE, xos_http_protocol::HTTP_RET_CODE_200 );
            pTags->set( xos_http_protocol::HTTP_CONTENT_TYPE, "text/html" );
            pTags->set( xos_http_protocol::HTTP_CONNECTION, "keep-alive" );
        }
        if( 0 == ret )
        {
            m_pProperty = mgr::container()->box()->get_str_prop();
            prop()->set_str_prop("tags", tags(), false);
        }
        return ret;
    }

    int response::term()
    {
        int ret = 0;
        xos_stl::release_interface( m_pProperty );
        xos_stl::release_interface( m_pCookies );
        xos_stl::release_interface( m_pHttpCompose );
        xos_stl::release_interface( m_pBodyList );
        xos_stl::release_interface( m_pSendList );
        xos_stl::release_interface( m_pTags );
        init_data();
        return ret;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //

} // xos_http
